package com.megalith.vo;

import com.megalith.constant.ResultCode;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description 接口返回数据格式
 * @Author lfp
 * @Date 2022/7/15
 * @Version 1.0
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回代码
     */
    private Integer code = 0;

    /**
     * 返回处理消息
     */
    private String message = "操作成功！";



    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    public static <T> Result<T> sysError(String msg) {
        return (Result<T>) sysError(ResultCode.SYS_ERROR.getCode(), msg);
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = ResultCode.SYS_SUCCESS.getCode();
        this.success = true;
        return this;
    }


    public static <T> Result<T> ok() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ResultCode.SYS_SUCCESS.getCode());
        r.setMessage(ResultCode.SYS_SUCCESS.getDes());
        return r;
    }

    public static <T> Result<T> ok(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ResultCode.SYS_SUCCESS.getCode());
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ResultCode.SYS_SUCCESS.getCode());
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> ok(T data,String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setResult(data);
        r.setCode(ResultCode.SYS_SUCCESS.getCode());
        r.setMessage(msg);
        return r;
    }



    public static <T> Result<T> error(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(ResultCode.SYS_ERROR.getCode());
        r.setMessage(ResultCode.SYS_ERROR.getDes());
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> sysError(int code, String msg) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }
}
